import logging
import time


from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

from page_object.create_queue import CreateQueue
from page_object.data_table import DataTable
from page_object.queuedatareport import QueueDatareport
from tools.base_page import BasePage
from selenium.webdriver.support import expected_conditions as EC


class DataQuery(BasePage):

    _search_view = (By.CSS_SELECTOR,".ant-input-search-button>i")
    #_base_url = 'http://124.133.43.229:88/mrpv2/#/dataResourceDirectory'
    _base_url = 'http://172.16.10.21/mrpv2/#/dataResourceDirectory'


    def expert_query_only_rule(self,text_value):  # 高级查询
        self.expert_query_action(text_value)
        self.open_yaml_element('../data/index.yaml', 'select_locator', is_elements=1, ele_num=0)
        time.sleep(5)
        return DataTable(self.driver)



    def expert_query_action(self,text_value):
        '''
        添加规则
        :param text_value:
        :return:
        '''
        time.sleep(2)
        self.verify_wait_click(('xpath','//*[@id="app"]/section/div/div[2]/main/div[1]/div/div[1]/div[2]/div/div/div/div[1]/div[1]/div'))
        self.driver.get(self._base_url)
        add_rule = self.only_read_yaml('../data/index.yaml','add_rule')
        self.find(add_rule).click()
        self.open_yaml_element('../data/index.yaml', 'option_one', is_elements=1,ele_num=0)
        time.sleep(3)
        locator = ("body>div:nth-of-type(3)>div>div>div>ul")
        # 若要对页面中的内嵌窗口中的滚动条进行操作，要先定位到该内嵌窗口，在进行滚动条操作
        try:
            time.sleep(2)
            self.driver.execute_script(f'document.querySelector("{locator}").scrollTop=10000')
            time.sleep(2)
        except:
            logging.info('scrollTop执行失败了，重试一下')
            time.sleep(2)
            self.driver.execute_script(f'document.querySelector("{locator}").scrollTop=10000')
            try:
                time.sleep(2)
                self.driver.execute_script(f'document.querySelector("{locator}").scrollTop=10000')
            except:
                time.sleep(2)
                self.driver.execute_script(f'document.querySelector("{locator}").scrollTop=10000')
            time.sleep(2)
        # 患者就诊记录的定位器
        huanzhe_locator = ('body>div:nth-of-type(3)>div>div>div>ul>li:nth-of-type(12)')
        self.verify_wait_click(('css selector',huanzhe_locator))
        self.selector_execute_script(huanzhe_locator)
        time.sleep(1)
        # 请选择变量
        self.open_yaml_element('../data/index.yaml', 'option_one', is_elements=1,ele_num=1)
        # 请选择变量中的性别
        self.driver.execute_script("document.querySelector('body>div:nth-of-type(4)>div>div>div>ul>li:nth-of-type(4)').click()")
        # 请选择规则
        self.open_yaml_element('../data/index.yaml', 'option_one', is_elements=1,ele_num=2)
        # 请选择规则中的等于
        self.driver.execute_script("document.querySelector('body>div:nth-of-type(5)>div>div>div>ul>li:nth-of-type(1)').click()")
        # 请输入值
        self.open_yaml_element('../data/index.yaml','input_value',content=text_value)
        time.sleep(2)



    def exist_readonly_view(self):
        '''
        设置开始 结束时间
        :return:
        '''
        for i in (1,3):
            js = "document.querySelector('.ant-input input:nth-child({})').removeAttribute('readonly')".format(i)
            self.driver.execute_script(js)
            if i == 3:
                js = "document.querySelector('.ant-input input:nth-child({})').value='2021-12-30'".format(i)
                self.driver.execute_script(js)
            else:
                js = "document.querySelector('.ant-input input:nth-child({})').value='2019-09-20'".format(i)
                self.driver.execute_script(js)







    def select_query(self):
        self.repeatedly_selcet()
        self.open_yaml_element('../data/index.yaml','select_locator')
        time.sleep(2)
        return DataTable(self.driver)



    def target_filtration_seacher_and_add(self,text_value):
        # 指标筛选搜索添加
        self.expert_query_action('男')
        self.open_yaml_element('../data/index.yaml','select_locator',is_elements=1,ele_num=1)  # 指标筛选click
        self.open_yaml_element('../data/index.yaml','input_view',content=text_value)
        ele_list = ['search_view','search_res','confirm_view']
        for ele in ele_list:
            self.open_yaml_element('../data/index.yaml',ele)
            time.sleep(1)
        return DataTable(self.driver)


    def cat_data_resource_cancel(self):
        # 查看数据资源报告  取消
        self.expert_query_action('男')
        self.open_yaml_element('../data/index.yaml','select_locator',is_elements=1,ele_num=2)
        time.sleep(2)
        self.open_yaml_element('../data/index.yaml', 'data_resource_cancel')
        return QueueDatareport(self.driver)


    def cat_data_resource_confirm(self,content):
        # 查看数据资源报告  确定
        self.expert_query_action('男')
        self.open_yaml_element('../data/index.yaml','select_locator',is_elements=1,ele_num=2)
        ele_key_list = ['data_resource_confirm', 'next_step', 'xuanxiang_one', 'next_ensure']
        for key in ele_key_list:
            self.open_yaml_element('../data/index.yaml', key)
            time.sleep(2)
        self.open_yaml_element('../data/index.yaml', 'apply_explain', content)
        submit_locator = self.only_read_yaml('../data/index.yaml','submit')
        eles = self.driver.find_elements(*submit_locator)
        eles[9].click()
        return QueueDatareport(self.driver)


    def create_queue_click(self):
        # 申请创建队列


        self.expert_query_action('男')
        time.sleep(2)
        create_locator = self.only_read_yaml('../data/index.yaml','create_queue')[1]  # 申请创建队列
        self.selector_execute_script(create_locator)
        return CreateQueue(self.driver)










